var Tools = {

};

//绘制
//function Draw(type, clampMode) {//type是区分绘制还是量测，clampModel是是否贴地
function Draw() {
    if ($("#toolbar").length > 0)
        $("#toolbar").remove();
    removeAllEntities();
    createDrawToolbars();
    //创建点、线、面处理对象
    var pointHandler = new Cesium.PointHandler(viewer);
    var polylineHandler = new Cesium.PolylineHandler(viewer);
    var polygonHandler = new Cesium.PolygonHandler(viewer);

    //viewer.scene.globe.depthTestAgainstTerrain = false;

    //点绘制结束回调事件
    pointHandler.drawCompletedEvent.addEventListener(function(point) {
        console.log("point is ..", point);
        //do something with this point
    });

    //线绘制结束回调事件
    polylineHandler.drawCompletedEvent.addEventListener(function(line) {
        //console.log("line is ..", line);
        //do something with this line
        var value = line.object.positions;
        var WGS84Arr = mapProjectionToWGS84(value);
        var length = getSpaceDistance(WGS84Arr);
        alert(length);
    });

    //面绘制结束回调事件
    polygonHandler.drawCompletedEvent.addEventListener(function(polygon) {
        console.log("polygon is ..", polygon);
        //do something with this polygon
        var value = polygon.object.positions;
        var WGS84Arr = mapProjectionToWGS84(value);
        var area = getSpaceArea(WGS84Arr);
        alert(area);
    });

    document.getElementById("point").onclick = function() {
        if (pointHandler.active || polylineHandler.active || polygonHandler.active) {
            swal("plz complete current drawing!");
            return;
        } else {
            pointHandler.activate();
        }
    }

    document.getElementById("line").onclick = function() {
        if (pointHandler.active || polylineHandler.active || polygonHandler.active) {
            swal("plz complete current drawing!");
            return;
        } else {
            polylineHandler.activate();
        }
    }

    document.getElementById("polygon").onclick = function() {
        if (pointHandler.active || polylineHandler.active || polygonHandler.active) {
            swal("plz complete current drawing!");
            return;
        } else {
            polygonHandler.activate();
        }
    }

}

//经纬度转笛卡尔坐标系
function turnCartesian3(pointArr) {
    let pointArrCartesian3 = [];
    for (let i = 0; i < pointArr.length; i++) {
        let car33;
        if (pointArr[i].length === 2)
            car33 = Cesium.Cartesian3.fromDegrees(pointArr[i][0], pointArr[i][1], 0);
        else
            car33 = Cesium.Cartesian3.fromDegrees(pointArr[i][0], pointArr[i][1], pointArr[i][2]);
        pointArrCartesian3.push(car33);
    }
    return pointArrCartesian3;
}

//地图坐标系坐标数组转经纬度数组
function mapProjectionToWGS84(pointArr) {
    var thisResult = [];
    for (var i = 0; i < pointArr.length; i++) {
        var ellipsoid = viewer.scene.globe.ellipsoid;
        var cartesian3 = new Cesium.Cartesian3(pointArr[i].x, pointArr[i].y, pointArr[i].z);
        var cartographic = ellipsoid.cartesianToCartographic(cartesian3);
        var lat = Cesium.Math.toDegrees(cartographic.latitude);
        var lng = Cesium.Math.toDegrees(cartographic.longitude);
        var alt = cartographic.height;
        var obj = { longitude: lng, latitude: lat, height: alt };
        thisResult.push(obj);
    }
    return thisResult;
}

function mapProjectionToWGS84_2(pointArr) {
    var thisResult = [];
    for (var i = 0; i < pointArr.length; i++) {
        var ellipsoid = viewer.scene.globe.ellipsoid;
        if (pointArr[i].length === 3)
            var cartesian3 = new Cesium.Cartesian3(pointArr[i][0], pointArr[i][1], pointArr[i][2]);
        else
            var cartesian3 = new Cesium.Cartesian3(pointArr[i][0], pointArr[i][1], 0);
        var cartographic = ellipsoid.cartesianToCartographic(cartesian3);
        var lat = Cesium.Math.toDegrees(cartographic.latitude);
        var lng = Cesium.Math.toDegrees(cartographic.longitude);
        var alt = cartographic.height;
        var obj = { longitude: lng, latitude: lat, height: alt };
        thisResult.push(obj);
    }
    return thisResult;
}

//WGS84转2000坐标
function WGS84To2000(pointArr, isPointArr) {
    var thisResult = [];
    if (isPointArr) {
        for (var i = 0; i < pointArr.length; i++) {
            thisResult.push(ol.proj.transform(pointArr[i], 'EPSG:4326', 'EPSG:3857'));
        }
    } else {
        for (var i = 0; i < pointArr.length / 2; i++) {
            thisResult.push(ol.proj.transform([pointArr[2 * i], pointArr[2 * i + 1]], 'EPSG:4326', 'EPSG:3857'));
        }
    }
    return thisResult;
}

//2000转WGS84坐标
function ToWGS84(pointArr, isPointArr) {
    var thisResult = [];
    if (isPointArr) {
        for (var i = 0; i < pointArr.length; i++) {
            thisResult.push(ol.proj.transform(pointArr[i], 'EPSG:3857', 'EPSG:4326'));
        }
    } else {
        for (var i = 0; i < pointArr.length / 2; i++) {
            thisResult.push(ol.proj.transform([pointArr[2 * i], pointArr[2 * i + 1]], 'EPSG:3857', 'EPSG:4326'));
        }
    }
    return thisResult;
}

//空间两点距离计算函数
function getSpaceDistance(positions) {
    var dis = 0;
    var geodesic = new Cesium.EllipsoidGeodesic();
    for (var i = 0; i < positions.length - 1; i++) {
        geodesic.setEndPoints(positions[i], positions[i + 1]);
        var s = geodesic.surfaceDistance;
        //返回两点之间的距离
        s = Math.sqrt(Math.pow(s, 2) + Math.pow(positions[i].height - positions[i + 1].height, 2));
        dis = dis + s;
    }
    return dis;
}


//计算多边形面积
function computeSignedArea(path) {
    let radius = 6371009
    let len = path.length;
    if (len < 3) return 0;
    let total = 0;
    let prev = path[len - 1];
    let prevTanLat = Math.tan(((Math.PI / 2 - prev.lat / 180 * Math.PI) / 2));
    let prevLng = (prev.lon) / 180 * Math.PI;
    for (let i in path) {
        let tanLat = Math.tan((Math.PI / 2 -
            (path[i].lat) / 180 * Math.PI) / 2);
        let lng = (path[i].lon) / 180 * Math.PI;
        total += polarTriangleArea(tanLat, lng, prevTanLat, prevLng);
        prevTanLat = tanLat;
        prevLng = lng;
    }
    return Math.abs(total * (radius * radius));
}

function polarTriangleArea(tan1, lng1, tan2, lng2) {
    let deltaLng = lng1 - lng2;
    let t = tan1 * tan2;
    return 2 * Math.atan2(t * Math.sin(deltaLng), 1 + t * Math.cos(deltaLng));
}